﻿Public Class FormMain
    Dim objUser As New clsUser
    Dim objCA As New clsCPCK
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        connectDB()
        getUserListByCJ()
        setDgvData()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_CHP.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(9)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        If dtUser.Rows.Count > 0 Then
            cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
        End If
        cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
        cmbOpers.Properties.ShowButtons = False
        cmbOpers.Properties.ShowPopupCloseButton = False
    End Sub

    Public Sub setDgvData()
        Dim dtCacheArea As DataTable = objCA.getListByStatus("0")
        dtCacheArea.Columns.Add("status_name")
        dtCacheArea.Columns.Add("area_name")
        If dtCacheArea.Rows.Count > 0 Then
            For Each row As DataRow In dtCacheArea.Rows
                If row("status") = "0" Then
                    row("status_name") = "在库"
                Else
                    row("status_name") = "已出库"
                End If
                If DBNull.Value.Equals(row("area")) = False Then
                    If row("area") = "ZAGZ01" Then
                        row("area_name") = "隔震成品库"
                    End If
                    If row("area") = "ZAGZ01A" Then
                        row("area_name") = "隔震成品库_A区"
                    End If
                    If row("area") = "ZAGZ01B" Then
                        row("area_name") = "隔震成品库_B区"
                    End If
                    If row("area") = "ZAGZ01C" Then
                        row("area_name") = "隔震成品库_C区"
                    End If
                    If row("area") = "ZAGZ01D" Then
                        row("area_name") = "隔震成品库_D区"
                    End If
                    If row("area") = "ZAGZ01E" Then
                        row("area_name") = "隔震成品库_E区"
                    End If
                Else
                    row("area_name") = ""
                End If

            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtCacheArea
        lblCount.Text = dgv.Rows.Count
        Dim n_count As Object = dtCacheArea.Compute("count(prod_id)", " into_date <= '" & Now.ToShortDateString() & " 23:59:59.999' and into_date >= '" & Now.ToShortDateString() & " 00:00:00.000'")
        If n_count IsNot DBNull.Value Then
            lblCountToday.Text = n_count
        Else
            lblCountToday.Text = "0"
        End If
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        setDgvData()
    End Sub

    Private Sub btnCheckFinished_Click(sender As Object, e As EventArgs) Handles btnCheckFinished.Click
        Dim frm As New FormCheckFinished()
        frm.ShowDialog()
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        Dim formRk As New FormRk()
        formRk.usr = cmbOpers.Text.Trim()
        formRk.ShowDialog(Me)
    End Sub

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        Dim frm As New FormRkMes()
        frm.usr = cmbOpers.Text.Trim()
        frm.ShowDialog(Me)
    End Sub

    Private Sub btnRkMesList_Click(sender As Object, e As EventArgs) Handles btnRkMesList.Click
        Dim frm As New FormRkMesList()
        frm.ShowDialog(Me)
    End Sub

    Private Sub btnDELIVER_Click(sender As Object, e As EventArgs) Handles btnDELIVER.Click
        Dim formDeliver As New FormDeliver()
        formDeliver.usr = cmbOpers.Text.Trim()
        formDeliver.ShowDialog(Me)
    End Sub

    Private Sub btnWarehouseCHPList_Click(sender As Object, e As EventArgs) Handles btnWarehouseCHPList.Click
        Dim frm As New FormWarehouseCHPList()
        frm.ShowDialog(Me)
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class
